Return to start page
Core/Interface/Struct Arrow Keys.j
1 library AStructCoreInterfaceArrowKeys
2
3 /**
4 * Functions following this function interface can be used as keypress event responses.
5 * @param key Parameter values: 0-up, 1-down, 2-right, 3-left
6 * @param pressed true-key was pressed, false-key was released
7 */
8 function interface AArrowKeysOnPressAction takes AArrowKeys arrowKeys, integer key, boolean pressed returns nothing
9
10 /**
11 * @author Anitarf
12 * @author Tamino Dauth
13 * @link http://www.wc3c.net/showthread.php?t=101271
14 */
15 struct AArrowKeys
16 public static constant integer keyUp = 0
17 public static constant integer keyDown = 1
18 public static constant integer keyLeft = 2
19 public static constant integer keyRight = 3
20 /**
21 * the following constant determines the behaviour of the vertical and horizontal
22 * variables. For example, if a player presses the up key and then presses the down
23 * key afterwards while still holding the up key, the vertical variable will be set
24 * to -1. Then, if the player releases the down key while still keeping the up key
25 * pressed, if RESUME_PREVIOUS_KEY is true the vertical variable will be set back
26 * to 1, else it will be set to 0.
27 */
28 //static start members
29 private static boolean m_resumePreviousKey
30 //static dynamic members
31 /**
32 * these are the external functions that get called when an arrow key is pressed/released
33 * you can set these variables to your functions that follow the Event function interface
34 * you don't need a different function for each event, you can set all these variables to
35 * a single function, since the function's parameters tell you what event occured.
36 */
37 private static AArrowKeysOnPressAction m_onPressUpAction
38 private static AArrowKeysOnPressAction m_onReleaseUpAction
39 private static AArrowKeysOnPressAction m_onPressDownAction
40 private static AArrowKeysOnPressAction m_onReleaseDownAction
41 private static AArrowKeysOnPressAction m_onPressLeftAction
42 private static AArrowKeysOnPressAction m_onReleaseLeftAction
43 private static AArrowKeysOnPressAction m_onPressRightAction
44 private static AArrowKeysOnPressAction m_onReleaseRightAction
45 //static members
46 private static trigger m_pressUpTrigger
47 private static trigger m_releaseUpTrigger
48 private static trigger m_pressDownTrigger
49 private static trigger m_releaseDownTrigger
50 private static trigger m_pressLeftTrigger
51 private static trigger m_releaseLeftTrigger
52 private static trigger m_pressRightTrigger
53 private static trigger m_releaseRightTrigger
54 private static thistype array m_playerArrowKeys[12] /// @todo @global bj_MAX_PLAYERS, vJass bug.
55 //dynamic members
56 // these are the "quick press" variables. They work similarly to the variables above,
57 // except that they aren't set to 0/false when a key is released. If you are checking
58 // the above variables on a periodic timer, you could miss a keypress if a player
59 // quickly presses and releases a key, the variables below allow you to catch such
60 // quick keypresses. Note that you must set these variables to 0/false yourself once
61 // you check them or they'll remain permanently set to 1/-1/true. Basicaly these
62 // variables tell you if a key has been pressed since you last set them to 0/false.
63 private integer m_verticalQuickPress
64 private integer m_horizontalQuickPress
65 private boolean m_upQuickPress
66 private boolean m_downQuickPress
67 private boolean m_leftQuickPress
68 private boolean m_rightQuickPress
69 //start members
70 private player m_player
71 //members
72 // this tells you the status of the arrow keys in the two directions for each player
73 // index 0 holds the values for player 1 (red), index 11 for player 12 (brown)
74 // a value of 0 means no keys pressed, 1 means right/up, -1 means left/down
75 // do not change the value of these variables
76 private integer m_vertical
77 private integer m_horizontal
78 // this tells you the status of each arrow key individualy for each player
79 // index 0 holds the values for player 1 (red), index 11 for player 12 (brown)
80 // this is basicaly the same as vertical/horizontal, you can use whichever you want
81 // do not change the value of these variables
82 private boolean m_up
83 private boolean m_down
84 private boolean m_left
85 private boolean m_right
86
87 //dynamic members
88
89 public method setVerticalQuickPress takes integer verticalQuickPress returns nothing
90 set this.m_verticalQuickPress = verticalQuickPress
91 endmethod
92
93 public method verticalQuickPress takes nothing returns integer
94 return this.m_verticalQuickPress
95 endmethod
96
97 public method setHorizontalQuickPress takes integer horizontalQuickPress returns nothing
98 set this.m_horizontalQuickPress = horizontalQuickPress
99 endmethod
100
101 public method horizontalQuickPress takes nothing returns integer
102 return this.m_horizontalQuickPress
103 endmethod
104
105 public method setUpQuickPress takes boolean upQuickPress returns nothing
106 set this.m_upQuickPress = upQuickPress
107 endmethod
108
109 public method upQuickPress takes nothing returns boolean
110 return this.m_upQuickPress
111 endmethod
112
113 public method setDownQuickPress takes boolean downQuickPress returns nothing
114 set this.m_downQuickPress = downQuickPress
115 endmethod
116
117 public method downQuickPress takes nothing returns boolean
118 return this.m_downQuickPress
119 endmethod
120
121 public method setLeftQuickPress takes boolean leftQuickPress returns nothing
122 set this.m_leftQuickPress = leftQuickPress
123 endmethod
124
125 public method leftQuickPress takes nothing returns boolean
126 return this.m_leftQuickPress
127 endmethod
128
129 public method setRightQuickPress takes boolean rightQuickPress returns nothing
130 set this.m_rightQuickPress = rightQuickPress
131 endmethod
132
133 public method rightQuickPress takes nothing returns boolean
134 return this.m_rightQuickPress
135 endmethod
136
137 //start members
138
139 public method player takes nothing returns player
140 return this.m_player
141 endmethod
142
143 //members
144
145 public method vertical takes nothing returns integer
146 return this.m_vertical
147 endmethod
148
149 public method horizontal takes nothing returns integer
150 return this.m_horizontal
151 endmethod
152
153 public method up takes nothing returns boolean
154 return this.m_up
155 endmethod
156
157 public method down takes nothing returns boolean
158 return this.m_down
159 endmethod
160
161 public method left takes nothing returns boolean
162 return this.m_left
163 endmethod
164
165 public method right takes nothing returns boolean
166 return this.m_right
167 endmethod
168
169 public static method create takes player user returns thistype
170 local thistype this = thistype.allocate()
171 //dynamic members
172 set this.m_verticalQuickPress = 0
173 set this.m_horizontalQuickPress = 0
174 set this.m_upQuickPress = false
175 set this.m_downQuickPress = false
176 set this.m_leftQuickPress = false
177 set this.m_rightQuickPress = false
178 //start members
179 set this.m_player = user
180 //members
181 set this.m_vertical = 0
182 set this.m_horizontal = 0
183 set this.m_up = false
184 set this.m_down = false
185 set this.m_left = false
186 set this.m_right = false
187 return this
188 endmethod
189
190 //members
191
192 private static method triggerActionKeyPressDown takes nothing returns nothing
193 local player triggerPlayer = GetTriggerPlayer()
194 local thistype this = thistype.playerArrowKeys(triggerPlayer)
195 set this.m_down = true
196 set this.m_vertical = -1
197 set this.m_downQuickPress = true
198 set this.m_verticalQuickPress = -1
199 if (thistype.m_onPressDownAction != 0) then
200 call thistype.m_onPressDownAction.execute(this, thistype.keyDown, true)
201 endif
202 set triggerPlayer = null
203 endmethod
204
205 private static method triggerActionKeyReleaseDown takes nothing returns nothing
206 local player triggerPlayer = GetTriggerPlayer()
207 local thistype this = thistype.playerArrowKeys(triggerPlayer)
208 set this.m_down = false
209 if (thistype.m_resumePreviousKey and this.m_up) then
210 set this.m_vertical = 1
211 else
212 set this.m_vertical = 0
213 endif
214 if (thistype.m_onReleaseDownAction != 0) then
215 call thistype.m_onReleaseDownAction.execute(this, thistype.keyDown, false)
216 endif
217 set triggerPlayer = null
218 endmethod
219
220 private static method triggerActionKeyPressUp takes nothing returns nothing
221 local player triggerPlayer = GetTriggerPlayer()
222 local thistype this = thistype.playerArrowKeys(triggerPlayer)
223 set this.m_up = true
224 set this.m_vertical = 1
225 set this.m_upQuickPress = true
226 set this.m_verticalQuickPress = 1
227 if (thistype.m_onPressUpAction != 0) then
228 call thistype.m_onPressUpAction.execute(this, thistype.keyUp, true)
229 endif
230 set triggerPlayer = null
231 endmethod
232
233 private static method triggerActionKeyReleaseUp takes nothing returns nothing
234 local player triggerPlayer = GetTriggerPlayer()
235 local thistype this = thistype.playerArrowKeys(triggerPlayer)
236 set this.m_up = false
237 if (thistype.m_resumePreviousKey and this.m_down) then
238 set this.m_vertical = -1
239 else
240 set this.m_vertical = 0
241 endif
242 if (thistype.m_onReleaseUpAction != 0) then
243 call thistype.m_onReleaseUpAction.execute(this, thistype.keyUp, false)
244 endif
245 set triggerPlayer = null
246 endmethod
247
248 private static method triggerActionKeyPressLeft takes nothing returns nothing
249 local player triggerPlayer = GetTriggerPlayer()
250 local thistype this = thistype.playerArrowKeys(triggerPlayer)
251 set this.m_left = true
252 set this.m_horizontal = -1
253 set this.m_leftQuickPress = true
254 set this.m_horizontalQuickPress = -1
255 if (thistype.m_onPressLeftAction != 0) then
256 call thistype.m_onPressLeftAction.execute(this, thistype.keyLeft, true)
257 endif
258 set triggerPlayer = null
259 endmethod
260
261 private static method triggerActionKeyReleaseLeft takes nothing returns nothing
262 local player triggerPlayer = GetTriggerPlayer()
263 local thistype this = thistype.playerArrowKeys(triggerPlayer)
264 set this.m_left = false
265 if (thistype.m_resumePreviousKey and this.m_right) then
266 set this.m_horizontal = 1
267 else
268 set this.m_horizontal = 0
269 endif
270 if (thistype.m_onReleaseLeftAction != 0) then
271 call thistype.m_onReleaseLeftAction.execute(this, thistype.keyLeft, false)
272 endif
273 set triggerPlayer = null
274 endmethod
275
276 private static method triggerActionKeyPressRight takes nothing returns nothing
277 local player triggerPlayer = GetTriggerPlayer()
278 local thistype this = thistype.playerArrowKeys(triggerPlayer)
279 set this.m_right = true
280 set this.m_horizontal = 1
281 set this.m_rightQuickPress = true
282 set this.m_horizontalQuickPress = 1
283 if (thistype.m_onPressRightAction != 0) then
284 call thistype.m_onPressRightAction.execute(this, thistype.keyRight, true)
285 endif
286 set triggerPlayer = null
287 endmethod
288
289 private static method triggerActionKeyReleaseRight takes nothing returns nothing
290 local player triggerPlayer = GetTriggerPlayer()
291 local thistype this = thistype.playerArrowKeys(triggerPlayer)
292 set this.m_right = false
293 if (thistype.m_resumePreviousKey and this.m_left) then
294 set this.m_horizontal = -1
295 else
296 set this.m_horizontal = 0
297 endif
298 if (thistype.m_onReleaseRightAction != 0) then
299 call thistype.m_onReleaseRightAction.execute(this, thistype.keyRight, false)
300 endif
301 set triggerPlayer = null
302 endmethod
303
304 public static method init takes boolean resumePreviousKey returns nothing
305 local triggeraction triggerAction
306 local integer i
307 local player user
308 local event triggerEvent
309 //static dynamic members
310 set thistype.m_onPressUpAction = 0
311 set thistype.m_onReleaseUpAction = 0
312 set thistype.m_onPressDownAction = 0
313 set thistype.m_onReleaseDownAction = 0
314 set thistype.m_onPressLeftAction = 0
315 set thistype.m_onReleaseLeftAction = 0
316 set thistype.m_onPressRightAction = 0
317 set thistype.m_onReleaseRightAction = 0
318 //static start members
319 set thistype.m_resumePreviousKey = resumePreviousKey
320 //static members
321 set thistype.m_pressUpTrigger = CreateTrigger()
322 set triggerAction = TriggerAddAction(thistype.m_pressUpTrigger, function thistype.triggerActionKeyPressUp)
323 set triggerAction = null
324 set thistype.m_releaseUpTrigger = CreateTrigger()
325 set triggerAction = TriggerAddAction(thistype.m_releaseUpTrigger, function thistype.triggerActionKeyReleaseUp)
326 set triggerAction = null
327 set thistype.m_pressDownTrigger = CreateTrigger()
328 set triggerAction = TriggerAddAction(thistype.m_pressDownTrigger, function thistype.triggerActionKeyPressDown)
329 set triggerAction = null
330 set thistype.m_releaseDownTrigger = CreateTrigger()
331 set triggerAction = TriggerAddAction(thistype.m_releaseDownTrigger, function thistype.triggerActionKeyReleaseDown)
332 set triggerAction = null
333 set thistype.m_pressLeftTrigger = CreateTrigger()
334 set triggerAction = TriggerAddAction(thistype.m_pressLeftTrigger, function thistype.triggerActionKeyPressLeft)
335 set triggerAction = null
336 set thistype.m_releaseLeftTrigger = CreateTrigger()
337 set triggerAction = TriggerAddAction(thistype.m_releaseLeftTrigger, function thistype.triggerActionKeyReleaseLeft)
338 set triggerAction = null
339 set thistype.m_pressRightTrigger = CreateTrigger()
340 set triggerAction = TriggerAddAction(thistype.m_pressRightTrigger, function thistype.triggerActionKeyPressRight)
341 set triggerAction = null
342 set thistype.m_releaseRightTrigger = CreateTrigger()
343 set triggerAction = TriggerAddAction(thistype.m_releaseRightTrigger, function thistype.triggerActionKeyReleaseRight)
344 set triggerAction = null
345 set i = 0
346 loop
347 exitwhen (i == bj_MAX_PLAYERS)
348 set user = Player(i)
349 set triggerEvent = TriggerRegisterPlayerEvent(thistype.m_pressUpTrigger, user, EVENT_PLAYER_ARROW_UP_DOWN)
350 set triggerEvent = null
351 set triggerEvent = TriggerRegisterPlayerEvent(thistype.m_releaseUpTrigger, user, EVENT_PLAYER_ARROW_UP_UP)
352 set triggerEvent = null
353 set triggerEvent = TriggerRegisterPlayerEvent(thistype.m_pressDownTrigger, user, EVENT_PLAYER_ARROW_DOWN_DOWN)
354 set triggerEvent = null
355 set triggerEvent = TriggerRegisterPlayerEvent(thistype.m_releaseDownTrigger, user, EVENT_PLAYER_ARROW_DOWN_UP)
356 set triggerEvent = null
357 set triggerEvent = TriggerRegisterPlayerEvent(thistype.m_pressLeftTrigger, user, EVENT_PLAYER_ARROW_LEFT_DOWN)
358 set triggerEvent = null
359 set triggerEvent = TriggerRegisterPlayerEvent(thistype.m_releaseLeftTrigger, user, EVENT_PLAYER_ARROW_LEFT_UP)
360 set triggerEvent = null
361 set triggerEvent = TriggerRegisterPlayerEvent(thistype.m_pressRightTrigger, user, EVENT_PLAYER_ARROW_RIGHT_DOWN)
362 set triggerEvent = null
363 set triggerEvent = TriggerRegisterPlayerEvent(thistype.m_releaseRightTrigger, user, EVENT_PLAYER_ARROW_RIGHT_UP)
364 set triggerEvent = null
365 set user = null
366 set thistype.m_playerArrowKeys[i] = 0
367 set i = i + 1
368 endloop
369 endmethod
370
371 public static method cleanUp takes nothing returns nothing
372 local integer i
373 //static members
374 call DestroyTrigger(thistype.m_pressUpTrigger)
375 set thistype.m_pressUpTrigger = null
376 call DestroyTrigger(thistype.m_releaseUpTrigger)
377 set thistype.m_releaseUpTrigger = null
378 call DestroyTrigger(thistype.m_pressDownTrigger)
379 set thistype.m_pressDownTrigger = null
380 call DestroyTrigger(thistype.m_releaseDownTrigger)
381 set thistype.m_releaseDownTrigger = null
382 call DestroyTrigger(thistype.m_pressLeftTrigger)
383 set thistype.m_pressLeftTrigger = null
384 call DestroyTrigger(thistype.m_releaseLeftTrigger)
385 set thistype.m_releaseLeftTrigger = null
386 call DestroyTrigger(thistype.m_pressRightTrigger)
387 set thistype.m_pressRightTrigger = null
388 call DestroyTrigger(thistype.m_releaseRightTrigger)
389 set thistype.m_releaseRightTrigger = null
390 set i = 0
391 loop
392 exitwhen (i == bj_MAX_PLAYERS)
393 if (thistype.m_playerArrowKeys[i] != 0) then
394 call thistype.m_playerArrowKeys[i].destroy()
395 set thistype.m_playerArrowKeys[i] = 0
396 endif
397 set i = i + 1
398 endloop
399 endmethod
400
401 //static dynamic members
402
403 public static method setOnPressUpAction takes AArrowKeysOnPressAction onPressUpAction returns nothing
404 set thistype.m_onPressUpAction = onPressUpAction
405 endmethod
406
407 public static method onPressUpAction takes nothing returns AArrowKeysOnPressAction
408 return thistype.m_onPressUpAction
409 endmethod
410
411 public static method setOnReleaseUpAction takes AArrowKeysOnPressAction onReleaseUpAction returns nothing
412 set thistype.m_onReleaseUpAction = onReleaseUpAction
413 endmethod
414
415 public static method onReleaseUpAction takes nothing returns AArrowKeysOnPressAction
416 return thistype.m_onReleaseUpAction
417 endmethod
418
419 public static method setOnPressDownAction takes AArrowKeysOnPressAction onPressDownAction returns nothing
420 set thistype.m_onPressDownAction = onPressDownAction
421 endmethod
422
423 public static method onPressDownAction takes nothing returns AArrowKeysOnPressAction
424 return thistype.m_onPressDownAction
425 endmethod
426
427 public static method setOnReleaseDownAction takes AArrowKeysOnPressAction onReleaseDownAction returns nothing
428 set thistype.m_onReleaseDownAction = onReleaseDownAction
429 endmethod
430
431 public static method onReleaseDownAction takes nothing returns AArrowKeysOnPressAction
432 return thistype.m_onReleaseDownAction
433 endmethod
434
435 public static method setOnPressLeftAction takes AArrowKeysOnPressAction onPressLeftAction returns nothing
436 set thistype.m_onPressLeftAction = onPressLeftAction
437 endmethod
438
439 public static method onPressLeftAction takes nothing returns AArrowKeysOnPressAction
440 return thistype.m_onPressLeftAction
441 endmethod
442
443 public static method setOnReleaseLeftAction takes AArrowKeysOnPressAction onReleaseLeftAction returns nothing
444 set thistype.m_onReleaseLeftAction = onReleaseLeftAction
445 endmethod
446
447 public static method onReleaseLeftAction takes nothing returns AArrowKeysOnPressAction
448 return thistype.m_onReleaseLeftAction
449 endmethod
450
451 public static method setOnPressRightAction takes AArrowKeysOnPressAction onPressRightAction returns nothing
452 set thistype.m_onPressRightAction = onPressRightAction
453 endmethod
454
455 public static method onPressRightAction takes nothing returns AArrowKeysOnPressAction
456 return thistype.m_onPressRightAction
457 endmethod
458
459 public static method setOnReleaseRightAction takes AArrowKeysOnPressAction onReleaseRightAction returns nothing
460 set thistype.m_onReleaseRightAction = onReleaseRightAction
461 endmethod
462
463 public static method onReleaseRightAction takes nothing returns AArrowKeysOnPressAction
464 return thistype.m_onReleaseRightAction
465 endmethod
466
467 //static methods
468
469 public static method playerArrowKeys takes player user returns thistype
470 if (thistype.m_playerArrowKeys[GetPlayerId(user)] == 0) then
471 set thistype.m_playerArrowKeys[GetPlayerId(user)] = thistype.create(user)
472 endif
473 return thistype.m_playerArrowKeys[GetPlayerId(user)]
474 endmethod
475 endstruct
476
477 endlibrary